<?php
// запрет прямого доступа
defined('_VALID_MOS') or die();
//настройки
$showAllDirectories = $params->get('showAllDirectories', 1);
$directoryId = $params->get('directoryId', 0);
$showDirName = $params->get('showDirName', 0);

$q = " SELECT id, name ";
$q .= " FROM #__boss_config ";
if($showAllDirectories == 0) $q .= " WHERE id IN ($directoryId) ";
$q .= " ORDER BY name";
$database->setQuery($q);
$directories = $database->loadObjectList();

function filter($categories) {
    global $category_id;
    return ($categories['category_parent_id'] == $category_id);
}

function sortCategoryTreeArray($categories, $directory, $parent_id = 0, $i=0, $html='') {

    global $i, $category_id, $_SESSION;

    require_once( JPATH_BASE.'/components/com_boss/boss.tools.php' );

    $count = count($categories);
    $j = 0;
    foreach ($categories as $category) {
        if ($category["category_parent_id"] == $parent_id) {
            $category_id = $parent_id;
            //$i - счетчик всех категорий $j - счетчик категорий одного уровня
            $i++;

            //вычисляем кол-во дочерних категорий
            $child_cat = array_filter(&$categories, 'filter');
            $cat_count = count($child_cat);
            unset($child_cat);
            //записываем рекурисвно дочерние категории во временную переменную
            $html_tmp = sortCategoryTreeArray($categories, $directory, $category["category_id"], $i);
            $itemid = getBossItemid(intval($directory), intval($category["category_id"]));
            $link = sefRelToAbs('index.php?option=com_boss&task=show_category&catid=' . $category["category_id"] . '&directory=' . $directory . '&Itemid=' . $itemid);
            if ($parent_id != 0 && ($j == 0 || $cat_count == 0))
                $html.= '<ul>';
            $html.='<li><a class="mainlevel" href="' . $link . '">';
            $html.= $category["category_name"];
            $html.= '</a>';
            $html.= $html_tmp;
            unset($html_tmp);
            $html.= '</li>';
            if ($parent_id != 0 && ($j == $cat_count - 1 || $cat_count == 0))
                $html.= '</ul>';

            $j++;
        }
    }
    unset($categories);
    return $html;
}

function getCategoryTreeArray($directories, $showDirName) {
    $directory = 14;
    $database = database::getInstance();
    print '<div class="topmenu">';
    foreach ($directories as $directory) {
        $database->setQuery("SELECT id, parent, name, ordering" .
                " FROM #__boss_" . $directory->id . "_categories as c" .
                " WHERE c.published='1' ORDER BY parent, ordering, name");
        $category = $database->loadObjectList();

        $categories = Array();
        // Transfer the Result into a searchable Array
        $i = 0;
        foreach ($category as $rows) {
            $categories[$i]["category_id"] = $rows->id;
            $categories[$i]["category_parent_id"] = $rows->parent;
            $categories[$i]["category_name"] = stripslashes($rows->name);
            $categories[$i]["list_order"] = $rows->ordering;
            $i++;
        }
        if ($showDirName == 1) print '<div class="dirname">'.$directory->name.'</div>';
        print '<ul class="menulist-round">';
        print sortCategoryTreeArray($categories, $directory->id);
        print '</ul>';
    }
    print '</div>';
}
?>
<SCRIPT type="text/javascript">
    $(document).ready(function(){
        $('.topmenu ul li').hover(
        function() {

            $(this).find('ul:first').stop(true, true);
            $(this).find('ul:first').slideDown();
        },
        function() {
            $(this).find('ul:first').slideUp('fast');
        }
    );
    });
</SCRIPT>
<STYLE type="text/css">
    .topmenu {
        float:left;
    }
    .topmenu ul, .topmenu li{
        margin: 0;
        padding: 0;
        border-bottom: 1px dashed #CCC;
        color: #5B5B5B;
        font-size: 13px;
    }
    .topmenu ul li {
        list-style: none;
        position:relative;
        width:200px;
        background-color: #FFFFFF;
    }
    .topmenu ul li ul {
        display: none;
        position:absolute;
        top:0px;
        left:200px;
        border: 1px solid #C1C1C1;
        margin:1px;
    }
    .topmenu ul li a {
        text-decoration: none;
        display: block;
    }
    .topmenu ul li a:hover, .topmenu ul li a#active_menu {
        background: #F2F3E9;
        color: #BD3620;
    }
    .dirname {
        color: #D43015;
        font: normal normal normal 16px/normal 'Lucida Sans Unicode', Arial, 'Lucida Grande', Helvetica, Tahoma, Verdana, sans-serif;
    }
</STYLE>
<?php
getCategoryTreeArray($directories, $showDirName);
?>
<div style="clear:both; "></div>